<template>
  <!-- 考察记录 -->
  <basic-table
    :showSelectCol="false"
    :loading="tableInfo.loading"
    :columns="tableInfo.columns"
    :dataSource="tableInfo.list"
    :pagination="pagination"
    :tableHeight="460"
    @pageChange="handlePageChange"
    @pageSizeChange="handlePageSizeChange" />
</template>

<script>
import BasicTable from '@/components/supplier/BasicTable';
import {
  queryPlanDetailBySupplierId, // 考察计划列表(通过供应商id查询 分页)
} from '@/api/supplier/returnVisteManage';
import { INSPECTION_TARGET_OPTIONS } from '@/pages/supplier/constants/common'
import { list2Obj } from '@/pages/supplier/utils';
const TABLE_COLUMNS = [
  { title: '计划名称', key: 'planName' },
  { title: '出差地点', key: 'businessTripPlace' },
  { title: '出差时间', key: 'startTime' },
  { title: '出差人员', key: 'businessTripPerson' },
  { title: '考察目的',
    key: 'targetList',
    formatter (row, column, cellValue, index) {
      if (cellValue && cellValue.length) {
        return cellValue.map((i) => {
          return list2Obj(INSPECTION_TARGET_OPTIONS)[i]
        }).join()
      }
      return ''
    }
  },
  { title: '考察报告',
    key: 'reportId',
  },
  { title: '考察完成时间', key: 'inspectCompleteTime' },
]
export default {
  components: {
    BasicTable,
  },
  props: {
    supplierId: { // 供应商Id
      type: [Number, String],
      default: ''
    },
  },
  data () {
    return {
      fromPageName: '',
      tableInfo: {
        list: [],
        selection: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      pagination: {
        total: 0,
        current: 1,
        size: 10,
        pages: 0
      },
    }
  },
  created () {
    this.getTableData();
  },
  methods: {
    async getTableData (current = this.pagination.current, size = this.pagination.size) {
      this.tableInfo.loading = true
      const { supplierId } = this
      const { status, result = [] } = await queryPlanDetailBySupplierId({ supplierId: `${supplierId}`, current, size })
      this.tableInfo.loading = false
      if (status === '0' && result) {
        const { records: list, current, size, pages, total } = result
        this.pagination = Object.assign(this.pagination, { current, size, pages, total })
        this.setTableColumns()
        this.tableInfo.list = list
      }
    },
    setTableColumns () {
      const { supplierId } = this
      this.tableInfo.columns = TABLE_COLUMNS.map((i) => {
        if (i.key === 'reportId') {
          i.formatter = (row, column, cellValue, index) => {
            const { planId, targetList } = row
            return <router-link to={{ name: 'viewReport', query: { planId, supplierId, targetList, type: 'view' } }}>查看报告</router-link>
          }
        }
        return i
      })
    },
    handlePageChange (current) {
      this.getTableData(current)
    },
    handlePageSizeChange (size) {
      this.getTableData(1, size)
    }
  },
}
</script>

<style scoped>

</style>
